package com.ziroom.zifamily.zfms.web;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ziroom.zifamily.bussiness.common.result.Result;
import com.ziroom.zifamily.zfms.logic.conser.ConserContentLogic;
import com.ziroom.zifamily.zfms.pojo.vo.CensorContentVO;
import com.ziroom.zifamily.zfms.pojo.bo.CensorContentBO;
import com.ziroom.zifamily.zfms.pojo.dto.CensorReqDTO;
import com.ziroom.zifamily.zfms.service.conser.CensorContentService;
import com.ziroom.zifamily.zfms.common.result.PageResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * <PRE>
 * <BR>    修改记录
 * <BR>-----------------------------------------------
 * <BR>    修改日期 2018-11-28        修改人 xgh         修改内容 新增部分请求方法
 * </PRE>
 *
 * @author cuiyh9
 * @version 1.0
 * @Date Created in 2018年11月19日 18:16
 * @since 1.0
 */
@Slf4j
@Controller
@RequestMapping("censor")

public class CensorController {

    @Autowired
    private CensorContentService censorContentService;
    @Autowired
    private ConserContentLogic conserContentLogic;

    @Value("${censorservice-url}")
    private String censorServiceUrl;

    @Autowired
    private RestTemplate restTemplate;

    @RequestMapping("index")
    public String index(ModelMap model) {
        log.info("-index===========");
        model.put("name", "xgh");
        model.put("role", "admin");
        return "/index/index";
    }

    @RequestMapping("content")
    public String contentPage() {
        log.info("-contentPage===========");

        return "/censor/censor_content";
    }
    @RequestMapping("picture")
    public String picturePage() {
        log.info("-picturePage===========");

        return "/censor/censor_picture";
    }
    @RequestMapping("video")
    public String videoPage() {
        log.info("-videoPage===========");

        return "/censor/censor_video";
    }
    @RequestMapping("audio")
    public String audioPage() {
        log.info("-audioPage===========");

        return "/censor/censor_audio";
    }

    /**
     * 单条数据,批量数据更新
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping("/updateBatch")

    public Result updateBatch(@RequestBody CensorReqDTO request){
        if(request == null){
            return Result.failParamNull();
        }
        log.info("[updateBatch 请求对象的内容为：]，{}" + request.toString());
        //TODO 请求审核服务
        try{
            ResponseEntity<Result> responseEntity = conserContentLogic.sendRequestToCensorService(censorServiceUrl,request);
            Result result = responseEntity.getBody();
            log.info("[updateBatch 请求审核服务ok]，返回结果为：{}",result.toString());
            return result;
        }catch (Exception e){
            log.error("[updateBatch 请求审核服务异常]，error:{}",e);
            return Result.failSystemError("updateBatch 请求审核服务异常");
        }

    }



    /**
     * 分页条件查询
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "getAllCensorContent")
    public PageResult queryCensorContentByPage( CensorReqDTO request) {

        //TODO 异常处理
        if (request == null){
            return new PageResult();
        }
        if(request.getPage() == null){
            request.setPage(1);
        }
        log.info("[查询request 对象的内容为：]，{}" , request.toString());
        //1、获取分页信息
        int pageNum = request.getPage() != 0 ? request.getPage() : 1;
        int pageSize = request.getLimit() != 0 ? request.getLimit() : 10;
        //2、封装请求查询对象
        CensorContentBO query = new CensorContentBO();
        BeanUtils.copyProperties(request, query);
        log.info("[查询query 对象的内容为：]，{}",query.toString());
        //3、调用分页方法
        PageHelper.startPage(pageNum, pageSize);

        //4、调用查询方法并返回
        PageInfo<CensorContentVO> pageInfo = censorContentService.findCensorContentByQuery(query);
        //5、封装返回结果
        PageResult pageResult = new PageResult();
        pageResult.setRows(pageInfo.getList());
        pageResult.setTotal(pageInfo.getTotal());
        log.info("[查询结果为：]+{}", pageInfo.toString());
        return pageResult;
    }


}
